Import libraries

In [8]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
In [9]:
fig, ax = plt.subplots()
xdata, ydata = [], []
line, = plt.plot([], [], 'rx-', animated=True)
plt.close()

def init():
    ax.set_xlim(0, 2*np.pi)
    ax.set_ylim(-1, 1)
    return line,

def update(frame):
    xdata.append(frame)
    ydata.append(np.sin(frame))
    line.set_data(xdata, ydata)
    fig.suptitle('i={}'.format(len(xdata)))
    return line,

n = 32
frm = np.linspace(0, 2*np.pi, n)
ani = FuncAnimation(fig, update, frames=frm,
                    init_func=init, interval=100,
                    blit=True)

HTML(ani.to_jshtml())
Out[9]:


Once Loop Reflect
In [10]:
%%capture
plt.rcParams["animation.html"] = "jshtml"

t = np.linspace(0, 2*np.pi, num=100)
x = np.sin(t)

fig, ax = plt.subplots()
h = ax.axis([0, 2*np.pi, -1, 1])
l, = ax.plot([], [])

def animate(i):
    title = 'i = {}'.format(i)
    fig.suptitle(title)
    l.set_data(t[:i], x[:i])

ani = FuncAnimation(fig, animate, frames=len(t),
                    interval = 100)
In [11]:
ani
Out[11]:


Once Loop Reflect
In [12]:
%%capture
plt.rcParams["animation.html"] = "jshtml"

t = np.linspace(0, 2*np.pi, num=100)
x = np.sin(t)

fig, ax = plt.subplots()
h = ax.axis([0, 2*np.pi, -1, 1])
l, = ax.plot([], [])

def animate(i):
    title = 'i = {}'.format(i)
    fig.suptitle(title)
    x = np.sin(2 * np.pi * (t - 0.1 * i))
    l.set_data(t, x)

ani = FuncAnimation(fig, animate, frames=len(t),
                    interval = 100)
In [13]:
ani
Out[13]:


Once Loop Reflect